package com.pzb.market.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.pzb.market.mapper.MkDiscountMapper;
import com.pzb.market.domain.MkDiscount;
import com.pzb.market.service.IMkDiscountService;

/**
 * 店铺活动Service业务层处理
 * 
 * @author pzb
 * @date 2021-10-12
 */
@Service
public class MkDiscountServiceImpl implements IMkDiscountService 
{
    @Autowired
    private MkDiscountMapper mkDiscountMapper;

    /**
     * 查询店铺活动
     * 
     * @param id 店铺活动主键
     * @return 店铺活动
     */
    @Override
    public MkDiscount selectMkDiscountById(Long id)
    {
        MkDiscount mkDiscount = mkDiscountMapper.selectMkDiscountById(id);
        if ("1".equals(mkDiscount.getAvailableGoods())) {
            List<Long> classIds=mkDiscountMapper.selectClassIdByDiscount(id);
            mkDiscount.setClassIds(classIds);
        }
        return mkDiscount;
    }

    /**
     * 查询店铺活动列表
     * 
     * @param mkDiscount 店铺活动
     * @return 店铺活动
     */
    @Override
    public List<MkDiscount> selectMkDiscountList(MkDiscount mkDiscount)
    {
        return mkDiscountMapper.selectMkDiscountList(mkDiscount);
    }

    /**
     * 新增店铺活动
     * 
     * @param mkDiscount 店铺活动
     * @return 结果
     */
    @Override
    public int insertMkDiscount(MkDiscount mkDiscount)
    {
        return mkDiscountMapper.insertMkDiscount(mkDiscount);
    }

    /**
     * 修改店铺活动
     * 
     * @param mkDiscount 店铺活动
     * @return 结果
     */
    @Override
    public int updateMkDiscount(MkDiscount mkDiscount)
    {
        if("1".equals(mkDiscount.getAvailableGoods())){
            //删除原有分类
            mkDiscountMapper.deleteDiscountClassByDiscountId(mkDiscount.getId());
            //添加新的分类
            mkDiscountMapper.batchSaveDiscountClass(mkDiscount);
        }

        return mkDiscountMapper.updateMkDiscount(mkDiscount);
    }

    /**
     * 批量删除店铺活动
     * 
     * @param ids 需要删除的店铺活动主键
     * @return 结果
     */
    @Override
    public int deleteMkDiscountByIds(Long[] ids)
    {
        //删除商品分类关联
        mkDiscountMapper.deleteDiscountClassByDiscountIds(ids);
        //删除商品关联
        mkDiscountMapper.deleteMkDiscountGoodsByDiscountIds(ids);
        //删除赠品关联
        mkDiscountMapper.deleteMkDiscountGiftByDiscountIds(ids);

        return mkDiscountMapper.deleteMkDiscountByIds(ids);
    }

    @Override
    public int insertMkDiscountGoods(MkDiscount mkDiscount) {
        //删除原有
        mkDiscountMapper.deleteMkDiscountGoodsByDiscountId(mkDiscount.getId());
        return mkDiscountMapper.batchInsertDiscountGoods(mkDiscount);
    }

    @Override
    public int deleteMkDiscountGoodsByIds(Long[] ids) {
        return mkDiscountMapper.deleteMkDiscountGoodsByIds(ids);
    }

    @Override
    public int insertMkDiscountGift(MkDiscount mkDiscount) {
        //删除原有
        mkDiscountMapper.deleteMkDiscountGiftByDiscountId(mkDiscount.getId());
        return mkDiscountMapper.batchInsertDiscountGift(mkDiscount);
    }

    @Override
    public int deleteMkDiscountGiftByIds(Long[] ids) {
        return mkDiscountMapper.deleteMkDiscountGiftByIds(ids);
    }


}
